﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using DotNetStar.TheKing.Infrastructure;
using DotNetStar.TheKing.Services;
using DotNetStar.TheKing.Web.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DotNetStar.TheKing.Web.Pages.User.Setting
{
    /// <summary>
    /// 个人信息
    /// </summary>
    [OwnPage]
    public class InformationModel : PageModel
    {
        private readonly UserService _userService;
        public InformationModel(UserService userService)
        {
            _userService = userService;
        }
        /// <summary>
        /// 性别
        /// </summary>
        [BindProperty]
        [Display(Name = "性别")]
        public Gender? Gender { get; set; }
        /// <summary>
        /// 技术经验（年）
        /// </summary>
        [BindProperty]
        [Display(Name = "技术经验(年)")]
        public int? TechExpYear { get; set; }

        /// <summary>
        /// 个性签名
        /// </summary>
        [BindProperty]
        [Display(Name ="签名")]
        [StringLength(255,ErrorMessage ="{0}不能超过{1}个字")]
        public string Signature { get; set; }

        /// <summary>
        /// 年龄
        /// </summary>
        [BindProperty]
        public int Year { get; set; }
        [BindProperty]
        public int Month { get; set; }
        [BindProperty]
        public int Day { get; set; }

        [Display(Name = "允许邀请")]
        [BindProperty]
        public bool AllowInvite { get; set; }

        /// <summary>
        /// 邀请身价
        /// </summary>
        [BindProperty]
        [Display(Name = "邀请身价")]
        public int? InviteCost { get; set; }

        public Domain.User UserInfo { get; set; }

        public async Task<IActionResult> OnGetAsync()
        {
            var user = await _userService.GetByIdAsync(User.Identity.UserId());
            if (user == null)
            {
                return RedirectToPage("/Error", new { message = "当前用户不存在" });
            }
            this.UserInfo = user;
            this.TechExpYear = user.TechExpYear;
            this.Signature = user.Signature;
            this.Gender = user.Gender;
            this.InviteCost = user.InviteCost;

            if(this.InviteCost.HasValue && this.InviteCost.Value < 0)
            {
                this.AllowInvite = false;
            }
            else
            {
                this.AllowInvite = true;
            }

            var birth = user.Birth;
            if (birth.HasValue)
            {
                this.Year = birth.Value.Year;
                this.Month = birth.Value.Month;
                this.Day = birth.Value.Day;
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {

            if (!ModelState.IsValid)
            {
                return Page();
            }

            var user = await _userService.GetByIdAsync(User.Identity.UserId());
            if (user == null)
            {
                return RedirectToPage("/Error", new { message = "找不到当前用户" });
            }

            try
            {
                
                user.TechExpYear = this.TechExpYear;
                user.Birth = new DateTime(this.Year, this.Month, this.Day);
                user.Signature = this.Signature;
                user.Gender = this.Gender;

                if (this.AllowInvite)
                {
                    user.SetAllowInvitationCost(this.InviteCost);
                }
                else
                {
                    user.NotAllowInvitation();
                }


                await _userService.UpdateUserAsync(user);

                await HttpContext.UpdateAsync(user);
                this.UserInfo = user;
                ViewData["Success"] = true;
                return Page();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return Page();
            }
        }
    }
}